var util = require('utils/util');
var wxApi = require('utils/wxApi');
var wxRequest = require('utils/wxRequest');

//var url_https="http://www.cai.com/api/";
var url_https = "https://applet.chubangkj.com/api/";
var obj = null;

App({
	
  onLaunch (options) {
    // Do something initial when launch.
	if (wx.canIUse("getUpdateManager")) {
      const updateManager = wx.getUpdateManager();
      updateManager.onCheckForUpdate(function(res) {
       
        // 请求完新版本信息的回调
        if (res.hasUpdate) {
          updateManager.onUpdateReady(function() {
            wx.showModal({
              title: "更新提示",
              content: "新版本已经准备好，请重新启动",
			        showCancel: false,
              success(res) {
                if (res.confirm) {
                  // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
                  updateManager.applyUpdate();
                }
              }
            });
          });
          updateManager.onUpdateFailed(function() {
            // 新的版本下载失败
            wx.showModal({
              title: "已经有新版本了哟~",
              content: "新版本已经上线啦~，请您删除当前小程序，重新搜索打开哟~"
            });
          });
        }
      });
	  
		
		  
    } else {
      wx.showModal({
        title: "提示",
        content:
          "当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。"
      });
    }
	
  },	
	

  globalData: {
    url_https: url_https ,
	  defaultPageSize:20,
    share:{title:"",path:"pages/index/index",imageUrl:""},
    tmp:{},
    num:0,
	  empty_msg:{title:"空空如也",text:"暂时没有相关数据"}
		
  }
  ,getShare:function(){
      var obj = this.globalData.share;
      var pages = getCurrentPages();
      pages = pages[pages.length - 1];
      var route = pages.route;
      obj.path =  route; //.substr(5);
      var arrp=[];
      if(pages.hasOwnProperty("options")){
          for(var x in pages.options){
              arrp.push(x+"="+pages.options[x]);
          }
      }
      if(arrp.length >0){
          obj.path = obj.path +"?"+arrp.join("&");
      }

      if(pages.data.hasOwnProperty("_simg")){
          obj.imageUrl =pages.data._simg;
      }
      obj.success=function(){
        //util.alert("成功")
      }

      return obj;
  }
  ,getUserInfo:function(cb){
    var that = this;
	 
    return new Promise((resolve, reject) => {
		if (!that.globalData.user) {
			var user = wx.getStorageSync('user');
			if(!util.judge.isNull(user) && user != ""){
				that.globalData.user=user;
			}
		}
		
		if (that.globalData.user) {
            console.log('99.已存在');
            resolve(that.globalData.user);
        }else{
            console.log('0.开始登陆');
			 
            var getSetting = wxApi.wxgetSetting();
            getSetting().then(res => {
				var u = res.authSetting['scope.userInfo'];
			
				if( u== false ){
				    if(util.judge.isNull(u) || u == false){
				        u="u";
				        wx.redirectTo({
				            url: '../login/login'
				        });
				        throw util.stop();
				    }
                    wx.redirectTo({
                        url: '../login/login'
					});
                    throw util.stop();
				}

				var user = wx.getStorageSync('user');
				if(!util.judge.isNull(user)){
					that.globalData.user=user;
				}

				wx.checkSession({
				    success:() => {
				        if (that.globalData.user && that.globalData.user != null && typeof that.globalData.user == "object" ){
				          resolve(that.globalData.user);
				        }else{
				            //wx.clearStorageSync();//清除所有缓存
				            wx.redirectTo({
				                url: '../login/login'
				            });
				        }
				    },
				    fail:() => {
				        //wx.clearStorageSync();//清除所有缓存
				        wx.redirectTo({
				            url: '../login/login'
				        });
				    }
				});
				
            }) 
			.catch(res => {
			  console.log('7.err-'+res);
			  //util.alert_catch(res);
			})
			.finally(function (res) {
			  // wx.hideToast();
			  console.log('app.getuser.finally~')
			});
		}
    });
  }
  
})



//很重要
Promise.prototype.finally = function (callback) {
  let P = this.constructor;
  return this.then(
    value => P.resolve(callback()).then(() => value),
    reason => P.resolve(callback()).then(() => { throw reason })
  );
};
